package com.sky.es.util;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class PageResultDto<T> extends BaseResDto{
    /**总条数*/
    private int totalSize;
    /**当前页数*/
    private int page;
    /**每页条数*/
    private int rows;
    /**总页数*/
    private int totalPage;

    public PageResultDto() {
        this.rows = this.rows == 0 ? 10 : this.rows;
    }

    public PageResultDto(int rows, int page) {
        this.rows = rows;
        this.page = page;
    }

    public PageResultDto(int rows, int page, T bodyData) {
        this.rows = rows;
        this.page = page;
        this.bodyData = bodyData;
    }

    public PageResultDto(int rows, int page, int totalSize, T bodyData) {
        this.rows = rows;
        this.page = page;
        this.totalSize = totalSize;
        this.totalPage = totalSize%rows==0 ? totalSize/rows : totalSize/rows + 1;
        this.bodyData = bodyData;
    }

    public void setData(int totalSize, T bodyData) {
        this.totalSize = totalSize;
        this.totalPage = totalSize%rows==0 ? totalSize/rows : totalSize/rows + 1;
        this.bodyData = bodyData;
    }

    @Override
    public PageResultDto<T> success() {
        this.resultCode = SUCCESS;
        return this;
    }

    @Override
    public PageResultDto<T> failure(String msg) {
        this.resultCode = "--";
        this.resultMsg = msg;
        return this;
    }

    @Override
    public PageResultDto<T> failure(String code, String msg) {
        this.resultCode = code;
        this.resultMsg = msg;
        return this;
    }

    @Override
    public PageResultDto<T> success(String msg) {
        this.resultCode = SUCCESS;
        this.resultMsg = msg;
        return this;
    }

    @Override
    public T getBodyData() {
        return (T) bodyData;
    }
}
